package transport;

import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Rectangle;
import javax.swing.JLabel;

public class AckProgressWindow extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JLabel jLabel = null;
	private JLabel messageNumberLabel = null;
	private JLabel jLabel1 = null;
	private JLabel uploadSpeedLabel = null;
	private JLabel jLabel2 = null;
	private JLabel jLabel3 = null;
	private JLabel latencyLabel = null;
	private JLabel failuresLabel = null;
	private int speedUpdates = 0;

	/**
	 * This is the default constructor
	 */
	public AckProgressWindow(String destName) {
		super();
		initialize(destName);
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize(String destName) {
		this.setSize(316, 178);
		this.setContentPane(getJContentPane());
		this.setTitle(TransportLayer.myID + " - Simple ACK transfer to "
				+ destName);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			failuresLabel = new JLabel();
			failuresLabel.setBounds(new Rectangle(181, 106, 106, 19));
			failuresLabel.setText("0");
			latencyLabel = new JLabel();
			latencyLabel.setBounds(new Rectangle(134, 75, 153, 19));
			latencyLabel.setText("0");
			jLabel3 = new JLabel();
			jLabel3.setBounds(new Rectangle(15, 106, 149, 19));
			jLabel3.setText("Transmission failures");
			jLabel2 = new JLabel();
			jLabel2.setBounds(new Rectangle(16, 76, 103, 15));
			jLabel2.setText("Latency (ms)");
			uploadSpeedLabel = new JLabel();
			uploadSpeedLabel.setBounds(new Rectangle(164, 46, 123, 19));
			uploadSpeedLabel.setText("0");
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(16, 46, 140, 19));
			jLabel1.setText("Upload speed (bps)");
			messageNumberLabel = new JLabel();
			messageNumberLabel.setBounds(new Rectangle(207, 13, 79, 17));
			messageNumberLabel.setText("0");
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(16, 13, 181, 18));
			jLabel.setText("Current message in waiting");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(jLabel, null);
			jContentPane.add(messageNumberLabel, null);
			jContentPane.add(jLabel1, null);
			jContentPane.add(uploadSpeedLabel, null);
			jContentPane.add(jLabel2, null);
			jContentPane.add(jLabel3, null);
			jContentPane.add(latencyLabel, null);
			jContentPane.add(failuresLabel, null);
		}
		return jContentPane;
	}

	void updateLatencyAndSpeed(long rtt, long lastMsgSize) {
		float oldPing = Float.parseFloat(latencyLabel.getText());
		float ping = (speedUpdates * oldPing + (float) rtt)
				/ (speedUpdates + 1);
		latencyLabel.setText(Float.toString(ping));
		float oldUpload = Float.parseFloat(uploadSpeedLabel.getText());
		float upload = (speedUpdates * oldUpload + ((float) lastMsgSize / (float) rtt) * 1000)
				/ (speedUpdates + 1);
		uploadSpeedLabel.setText(Float.toString(upload));
		speedUpdates++;
	}

	void updateFailures() {
		failuresLabel.setText(Integer.toString((Integer.parseInt(failuresLabel
				.getText()) + 1)));
	}

	void updateCurrentMessage() {
		messageNumberLabel.setText(Integer.toString((Integer
				.parseInt(messageNumberLabel.getText()) + 1)));
	}

	void setEnded() {
		messageNumberLabel.setText("Transfer completed");
	}
} // @jve:decl-index=0:visual-constraint="10,10"
